package com.acegear.horizon.controllers;

import java.util.Optional;

import javax.transaction.Transactional;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import com.acegear.horizon.controllers.vo.ResultVO;
import com.acegear.horizon.domain.models.Club;
import com.acegear.horizon.domain.models.jpa.ClubBankCard;
import com.acegear.horizon.domain.models.jpa.ClubLabel;
import com.acegear.horizon.domain.repository.ClubRepository;
import com.acegear.horizon.domain.repository.jpa.ClubBankCardRepository;
import com.acegear.horizon.domain.repository.jpa.ClubLabelRepository;

/**
 * 
 * @author TreeJohn 17-05-11
 *
 */
@RestController
@RequestMapping("settings")
@Transactional
public class ClubSettingController {

	@Autowired
	private ClubRepository clubRepository;

	@Autowired
	private ClubLabelRepository clubLabelRepository;

	@Autowired
	private ClubBankCardRepository clubBankCardRepository;

	/**
	 * 查询俱乐部设置页展示信息
	 */
	@RequestMapping(value = "/getClubInfo", method = RequestMethod.GET)
	public ResultVO<?> getClub(Long clubId, Long userId) {
		Optional<Club> clubOptional = clubRepository.findByClubId(clubId);
		if (clubOptional.isPresent()) {
			Club club = clubOptional.get();
			return new ResultVO<>(club.getClubVO(userId));
		} else {
			return ResultVO.CLUB_NOT_FOUND;
		}
	}

	/**
	 * 保存俱乐部设置页展示信息
	 */
	@RequestMapping(value = "/saveClubInfo", method = RequestMethod.GET)
	public ResultVO<?> saveClub(Long userId, Long clubId, String clubName, String logo, String info, String background,
			String introduction) {
		Optional<Club> clubOptional = clubRepository.findByClubId(clubId);
		Club club = clubOptional.get();
		club.getBase().setClubName(clubName);
		club.getBase().setLogo(logo);
		club.getBase().setBackground(background);
		club.getInfo().setInfo(info);
//		club.getInfo().setIntroduction(introduction); // 暂时先不存
		club.save();

		return ResultVO.OPTION_SUCCESS;
	}

	/**
	 * 添加俱乐部标签
	 */
	@RequestMapping(value = "/addClubLabel", method = RequestMethod.GET)
	public ResultVO<?> addClubLabel(Long clubId, String labelName) {
		ClubLabel clubLabel = new ClubLabel(clubId, labelName);
		clubLabelRepository.save(clubLabel);
		return ResultVO.OPTION_SUCCESS;
	}

	/**
	 * 查询俱乐部标签列表
	 */
	@RequestMapping(value = "/getClubLabelList", method = RequestMethod.GET)
	public ResultVO<?> getClubLabelList(Long clubId) {
		return new ResultVO<>(clubLabelRepository.findClubLabelListByClubId(clubId));
	}

	// /**
	// * 查询俱乐部标签列表 by Name like
	// */
	// @RequestMapping(value = "/getClubLabelListByName", method =
	// RequestMethod.GET)
	// public ResultVO<?> getClubLabelListByName(String name) {
	// return new ResultVO<>(clubLabelRepository.findClubLabelListByName(name));
	// }

	/**
	 * 删除标签
	 */
	@RequestMapping(value = "/deleteClubLabel", method = RequestMethod.GET)
	public ResultVO<?> deleteClubLabel(Long id) {
		clubLabelRepository.delete(id);
		return ResultVO.OPTION_SUCCESS;
	}

	/**
	 * 批量删除标签 by IDs,暂时循环删除
	 * 
	 * @param ids用,分割
	 */
	@RequestMapping(value = "/deleteClubLabelByIds", method = RequestMethod.GET)
	public ResultVO<?> deleteClubLabelIds(String ids) {
		String[] idList = ids.split(",");
		for (int i = 0; i < idList.length; i++) {
			Long id = Long.parseLong(idList[i]);
			clubLabelRepository.delete(id);
		}
		return ResultVO.OPTION_SUCCESS;
	}

	/**
	 * 查询俱乐部银行帐号列表
	 */
	@RequestMapping(value = "/getClubBankCardList", method = RequestMethod.GET)
	public ResultVO<?> getClubBankCardList(Long clubId) {
		return new ResultVO<>(clubBankCardRepository.findClubBankCardListByClubId(clubId));
	}

	/**
	 * 添加俱乐部银行卡
	 */
	@RequestMapping(value = "/addClubBankCard", method = RequestMethod.GET)
	public ResultVO<?> addClubBankCard(Long clubId, String cardType, String accountName, String accountNo,
			String openPlace, String openBank, String subBranch) {
		ClubBankCard bankCard = new ClubBankCard(clubId, cardType, accountName, accountNo, openPlace, openBank,
				subBranch);
		clubBankCardRepository.save(bankCard);
		return ResultVO.OPTION_SUCCESS;
	}

	/**
	 * 删除俱乐部银行卡
	 */
	@RequestMapping(value = "/deleteBankCard", method = RequestMethod.GET)
	public ResultVO<?> deleteBankCard(Long id) {
		clubBankCardRepository.delete(id);
		return ResultVO.OPTION_SUCCESS;
	}
}
